package com.soft.util;

import java.io.Serializable;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.LocaleProvider;
import com.opensymphony.xwork2.TextProvider;
import com.opensymphony.xwork2.TextProviderFactory;
import com.opensymphony.xwork2.Validateable;
import com.opensymphony.xwork2.ValidationAware;
import com.opensymphony.xwork2.ValidationAwareSupport;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;

public class MyActionSupport implements Action, Validateable, ValidationAware, TextProvider,
		LocaleProvider, SessionAware, ServletRequestAware, ServletResponseAware, Serializable {

	public HttpServletRequest request;

	public HttpServletResponse response;

	public Map map;

	public String pnstr;

	public String rnstr;

	public Integer pn;

	public Integer rn;

	public int maxcount;

	public int maxpage;

	public int next;

	public int previous;

	public List listPageCout;

	public List listRn;

	protected static Logger LOG = LoggerFactory.getLogger(MyActionSupport.class);

	private final transient TextProvider textProvider = new TextProviderFactory().createInstance(
			getClass(), this);

	private final ValidationAwareSupport validationAware = new ValidationAwareSupport();

	public void setActionErrors(Collection<String> errorMessages) {
		validationAware.setActionErrors(errorMessages);
	}

	public Collection<String> getActionErrors() {
		return validationAware.getActionErrors();
	}

	public void setActionMessages(Collection<String> messages) {
		validationAware.setActionMessages(messages);
	}

	public Collection<String> getActionMessages() {
		return validationAware.getActionMessages();
	}

	/**
	 * @deprecated Use {@link #getActionErrors()}.
	 */
	@Deprecated
	public Collection<String> getErrorMessages() {
		return getActionErrors();
	}

	/**
	 * @deprecated Use {@link #getFieldErrors()}.
	 */
	@Deprecated
	public Map<String, List<String>> getErrors() {
		return getFieldErrors();
	}

	public void setFieldErrors(Map<String, List<String>> errorMap) {
		validationAware.setFieldErrors(errorMap);
	}

	public Map<String, List<String>> getFieldErrors() {
		return validationAware.getFieldErrors();
	}

	public Locale getLocale() {
		ActionContext ctx = ActionContext.getContext();
		if (ctx != null) {
			return ctx.getLocale();
		} else {
			LOG.debug("Action context not initialized");
			return null;
		}
	}

	public boolean hasKey(String key) {
		return textProvider.hasKey(key);
	}

	public String getText(String aTextName) {
		return textProvider.getText(aTextName);
	}

	public String getText(String aTextName, String defaultValue) {
		String value = null;
		try {
			ResourceBundle bundle = ResourceBundle.getBundle(defaultValue, ActionContext.getContext()
					.getLocale());
			value = bundle.getString(aTextName);
		} catch (Exception e) {
			return textProvider.getText(aTextName, defaultValue);
		}
		return value;
	}

	public String getText(String aTextName, String defaultValue, String obj) {
		return textProvider.getText(aTextName, defaultValue, obj);
	}

	public String getText(String aTextName, List<Object> args) {
		return textProvider.getText(aTextName, args);
	}

	public String getText(String key, String[] args) {
		return textProvider.getText(key, args);
	}

	public String getText(String aTextName, String defaultValue, List<Object> args) {
		return textProvider.getText(aTextName, defaultValue, args);
	}

	public String getText(String key, String defaultValue, String[] args) {
		String value = null;
		try {
			ResourceBundle bundle = ResourceBundle.getBundle(defaultValue, ActionContext.getContext()
					.getLocale());
			value = bundle.getString(key);
			value = MessageFormat.format(value, args);
		} catch (Exception e) {
			return textProvider.getText(key, defaultValue, args);
		}
		return value;
	}

	public String getText(String key, String defaultValue, List<Object> args, ValueStack stack) {
		return textProvider.getText(key, defaultValue, args, stack);
	}

	public String getText(String key, String defaultValue, String[] args, ValueStack stack) {
		return textProvider.getText(key, defaultValue, args, stack);
	}

	public ResourceBundle getTexts() {
		return textProvider.getTexts();
	}

	public ResourceBundle getTexts(String aBundleName) {
		return textProvider.getTexts(aBundleName);
	}

	public void addActionError(String anErrorMessage) {
		validationAware.addActionError(anErrorMessage);
	}

	public void addActionMessage(String aMessage) {
		validationAware.addActionMessage(aMessage);
	}

	public void addFieldError(String fieldName, String errorMessage) {
		validationAware.addFieldError(fieldName, errorMessage);
	}

	public String input() throws Exception {
		return INPUT;
	}

	public String doDefault() throws Exception {
		return SUCCESS;
	}

	/**
	 * A default implementation that does nothing an returns "success".
	 * <p/>
	 * Subclasses should override this method to provide their business logic.
	 * <p/>
	 * See also {@link com.opensymphony.xwork2.Action#execute()}.
	 * 
	 * @return returns {@link #SUCCESS}
	 * @throws Exception
	 *           can be thrown by subclasses.
	 */
	public String execute() throws Exception {
		return SUCCESS;
	}

	public boolean hasActionErrors() {
		return validationAware.hasActionErrors();
	}

	public boolean hasActionMessages() {
		return validationAware.hasActionMessages();
	}

	public boolean hasErrors() {
		return validationAware.hasErrors();
	}

	public boolean hasFieldErrors() {
		return validationAware.hasFieldErrors();
	}

	/**
	 * Clears field errors. Useful for Continuations and other situations where you might want to
	 * clear parts of the state on the same action.
	 */
	public void clearFieldErrors() {
		validationAware.clearFieldErrors();
	}

	/**
	 * Clears action errors. Useful for Continuations and other situations where you might want to
	 * clear parts of the state on the same action.
	 */
	public void clearActionErrors() {
		validationAware.clearActionErrors();
	}

	/**
	 * Clears messages. Useful for Continuations and other situations where you might want to clear
	 * parts of the state on the same action.
	 */
	public void clearMessages() {
		validationAware.clearMessages();
	}

	/**
	 * Clears all errors. Useful for Continuations and other situations where you might want to clear
	 * parts of the state on the same action.
	 */
	public void clearErrors() {
		validationAware.clearErrors();
	}

	/**
	 * Clears all errors and messages. Useful for Continuations and other situations where you might
	 * want to clear parts of the state on the same action.
	 */
	public void clearErrorsAndMessages() {
		validationAware.clearErrorsAndMessages();
	}

	/**
	 * A default implementation that validates nothing. Subclasses should override this method to
	 * provide validations.
	 */
	public void validate() {
	}

	@Override
	public Object clone() throws CloneNotSupportedException {
		return super.clone();
	}

	/**
	 * <!-- START SNIPPET: pause-method --> Stops the action invocation immediately (by throwing a
	 * PauseException) and causes the action invocation to return the specified result, such as
	 * {@link #SUCCESS}, {@link #INPUT}, etc.
	 * <p/>
	 * <p/>
	 * The next time this action is invoked (and using the same continuation ID), the method will
	 * resume immediately after where this method was called, with the entire call stack in the
	 * execute method restored.
	 * <p/>
	 * <p/>
	 * Note: this method can <b>only</b> be called within the {@link #execute()} method. <!-- END
	 * SNIPPET: pause-method -->
	 * 
	 * @param result
	 *          the result to return - the same type of return value in the {@link #execute()}
	 *          method.
	 */
	public void pause(String result) {
	}

	public void setSession(Map<String, Object> map) {
		this.map = map;
	}

	public void setServletRequest(HttpServletRequest request) {
		// System.out.println(""+request);
		this.request = request;
		page();
	}

	public void page() {
		rn = this.getInteger(rnstr, 15);
		pnstr = request.getParameter("pn");
		rnstr = request.getParameter("rn");
		pn = getInteger(pnstr, 1);
		maxpage = 1;
		if (maxcount > 0) {
			int maxpn = HandlePn.getPageMax(maxcount, rn);
			maxpage = maxpn;
			if (maxpn < pn)
				pn = maxpn;

		}
		this.next = HandlePn.getNext(pn, maxpage);
		this.previous = HandlePn.getPrevious(pn);
		listPageCout = new ArrayList();
		for (int i = 1; i <= maxpage; i++) {
			listPageCout.add(i);
		}
		listRn = new ArrayList();
		listRn.add(5);
		listRn.add(8);
		listRn.add(10);
		listRn.add(15);
		listRn.add(20);
		listRn.add(30);
		listRn.add(50);
	}

	public void page(int count) {
		if (count != 0)
			rn = this.getInteger(rnstr, count);
		else
			rn = this.getInteger(rnstr, 15);
		pnstr = request.getParameter("pn");
		rnstr = request.getParameter("rn");
		pn = getInteger(pnstr, 1);
		maxpage = 1;
		if (maxcount > 0) {
			int maxpn = HandlePn.getPageMax(maxcount, rn);
			maxpage = maxpn;
			if (maxpn < pn)
				pn = maxpn;

		}
		this.next = HandlePn.getNext(pn, maxpage);
		this.previous = HandlePn.getPrevious(pn);
		listPageCout = new ArrayList();
		for (int i = 1; i <= maxpage; i++) {
			listPageCout.add(i);
		}
		listRn = new ArrayList();
		listRn.add(5);
		listRn.add(8);
		listRn.add(10);
		listRn.add(15);
		listRn.add(20);
		listRn.add(30);
		listRn.add(50);
	}

	public void setServletResponse(HttpServletResponse response) {

		this.response = response;
	}

	public Integer getInteger(String str, Integer def) {
		Integer rint = getInteger(str);
		if (rint != null)
			return rint;
		return def;
	}

	public Integer getInteger(String str) {
		try {
			return new Integer(str.trim());
		} catch (Exception e) {
		}
		return null;
	}

	public int getMaxcount() {
		return maxcount;
	}

	public void setMaxcount(int maxcount) {
		this.maxcount = maxcount;
	}

	public int getMaxpage() {
		return maxpage;
	}

	public void setMaxpage(int maxpage) {
		this.maxpage = maxpage;
	}

	public Integer getPn() {
		return pn;
	}

	public void setPn(Integer pn) {
		this.pn = pn;
	}

	public String getPnstr() {
		return pnstr;
	}

	public void setPnstr(String pnstr) {
		this.pnstr = pnstr;
	}

	public Integer getRn() {
		return rn;
	}

	public void setRn(Integer rn) {
		this.rn = rn;
	}

	public String getRnstr() {
		return rnstr;
	}

	public void setRnstr(String rnstr) {
		this.rnstr = rnstr;
	}

	public int getNext() {
		return next;
	}

	public void setNext(int next) {
		this.next = next;
	}

	public int getPrevious() {
		return previous;
	}

	public void setPrevious(int previous) {
		this.previous = previous;
	}

	public List getListPageCout() {
		return listPageCout;
	}

	public void setListPageCout(List listPageCout) {
		this.listPageCout = listPageCout;
	}

	public List getListRn() {
		return listRn;
	}

	public void setListRn(List listRn) {
		this.listRn = listRn;
	}
}
